package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    ////根据条件查询规格参数集合
    @Override
    public List<SpecParamDTO> SpecParamByIds(Long categoryId, Long groupId, Boolean searching) {
        if(categoryId == null && groupId == null){
            // 条件不能都为空
            throw new LyException(400, "查询条件不能为空！");
        }
        List<SpecParam> list =
                query().eq(categoryId != null, "category_id", categoryId)
                        .eq(groupId != null, "group_id", groupId)
                        .eq(searching!=null,"searching", searching).list();
        return SpecParamDTO.convertEntityList(list);
    }

    //规格参数添加
    @Override
    @Transactional
    public void addSpecPram( SpecParamDTO specParamDTO) {
        //Dto转换po
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        //添加
        boolean save = save(specParam);
        if (save==false){
            throw new LyException(404,"规格参数添加失败");
        }
    }

    //规格参数修改
    @Override
    public void updateSpecPram(SpecParamDTO specParamDTO) {
        //DTO转换为po
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        //修改
        boolean b = updateById(specParam);
        if (b==false){
            throw  new LyException(500,"修改规格参数失败");
        }
    }


}
